// pages/orderdetail/orderdetail.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headimg
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.i){
      this.getdetail(options.i);
    }
    this.getsetting();
  },
  getsetting(){
    let setting = wx.getStorageSync("setting");
    setting.deposit =setting.deposit/100;
    setting.rent = setting.rent/100;
    let total = setting.deposit + setting.rent;
    this.setData({setting,total});
    // this.getdate(setting.borrowdate);
  },
  getdetail(i){
    let pages = getCurrentPages(), page = pages[pages.length - 2];
    let { list } = page.data,detail = list[i];
    this.getaddressbyid(detail.item.addressid);
    detail.rent = detail.total-detail.deposit;
    // detail['newtotal']=(detail.total/100).toFixed(2)
    console.log(detail,'detaildetail')
      detail.books.forEach((y, i) => {
        console.log(y, 'yyyy')
        if (y.goodsimg.indexOf("http") == -1) {
          y.thumbPicPath = headimg + y.goodsimg
          y['newprice']=(y.promotionprice/100).toFixed(2)
        }
      });
    
    this.setData({detail});
  },
  getaddressbyid(id){
    api.getaddressbyid({id},res=>{
      if(res.data.issuccess==1){
        let address = res.data.data;
        this.setData({address});
      }
    });
  },
  sub(e){
  let type=wx.getStorageSync('type'),{ordernumber,goodsid,total}=e.currentTarget.dataset,openid = wx.getStorageSync("openid"), setting = wx.getStorageSync("setting"),deposit = setting.deposit*100;
  total = total*100;
    if(goodsid!=''){
        let  subdata = {ordernumber,openid,total};
          api.prepay(subdata, res => {
            // console.log(res,'2222');
            if (res.succ == 1) {
              // if(type=="CONSUMER"){
              //   api.setvip(res =>{
              //     if(res.data.issuccess == 1){
              //       console.log(1111)
              //     }
              //   })
              // }
              api.afterpaysucc({ ordernumber, cost: total, goodsid, deposit }, res => {
                // console.log(3333,'3333');
                if (res.data.issuccess == 1) {
                console.log(4444,'4444');
                  wx.redirectTo({
                    url: '../shopOrder/shopOrder?o=2',
                  })
                }
              })
            // } else {
              // wx.redirectTo({
              //   url: '../order/order?o=0',
              // })
            }
            
          })
    }else{
      
    }
  },
  copy(e){
    wx.setClipboardData({
      data: e.currentTarget.dataset.num,
    })
  },
  makephone(){
    // let phone=app.logininfo.phone
    // console.log('13960823087')
    wx.makePhoneCall({
      phoneNumber: '13960823087,'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.logininfo.appletname,
      path: `pages/listen/listen?share=`+app.logininfo.userid,
    }
  }
})